package palmtop.com.palmtop.activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.widget.GridLayout;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageButton;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import net.grandcentrix.tray.AppPreferences;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import palmtop.com.palmtop.R;
import palmtop.com.palmtop.model.Clue;
import palmtop.com.palmtop.model.Reportpopulation;
import palmtop.com.palmtop.model.RoleMenu;
import palmtop.com.palmtop.util.Config;
import palmtop.com.palmtop.util.NetUtil;
import palmtop.com.palmtop.util.ToastUtil;

public class MainActivity extends BaseActivity {

    @BindView(R.id.downtaskBtn)
    ImageButton downtaskBtn;
    @BindView(R.id.histaskBtn)
    ImageButton histaskBtn;
    @BindView(R.id.populationBtn)
    ImageButton populationBtn;
    @BindView(R.id.clueBtn)
    ImageButton clueBtn;
    @BindView(R.id.mapBtn)
    ImageButton mapBtn;
    @BindView(R.id.kaoheBtn)
    ImageButton kaoheBtn;
    @BindView(R.id.mytaskBtn)
    ImageButton mytaskBtn;
    @BindView(R.id.addpopulationBtn)
    ImageButton addpopulationBtn;
    @BindView(R.id.addclueBtn)
    ImageButton addclueBtn;
    @BindView(R.id.mygrids)
    GridLayout mygrids;
    private long mBackPressed;
    private static final int TIME_INTERVAL = 2000;
//    private SharedPreferences sharedPreferences;
    String permission;
//    SharedPreferences permissionPreferences;
    private ArrayList<RoleMenu> arrayList;
    String[] arr;
    String role_id = "";
    String ssgrid = "";
    MyApplication app;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main, Config.toptitle, -1);
        app = (MyApplication) getApplication();
        app.addActivity(MainActivity.this);

        ButterKnife.bind(this);
        iniView();
    }

    private void iniView() {
//        sharedPreferences = getSharedPreferences("user",Context.MODE_PRIVATE);
//        permissionPreferences = getSharedPreferences("permission",Context.MODE_MULTI_PROCESS);

        AppPreferences appPreferences = new AppPreferences(MainActivity.this);
        permission = appPreferences.getString("permission","");
        String username = appPreferences.getString("loginname","");
        String userid = appPreferences.getString("userid","");
        role_id = appPreferences.getString("role_id","");
        ssgrid = appPreferences.getString("ssgrid","");
        //permission = appPreferences.getString("permission","");
        if(permission==null||permission.equals("")){
            //ToastUtil.show(getActivity(),"在这里执行的");
            getPermissionMenu(userid,username);
        }else{
            showMenu();
        }
    }

    public void getPermissionMenu(String userid,String username){
        RequestParams params = new RequestParams(Config.SERVER_URL+"sys/loginapi/getmenu");
        params.addBodyParameter("userid",userid);
        params.addBodyParameter("username",username);
        NetUtil.postData(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Gson gson = new Gson();
                Type listType = new TypeToken<ArrayList<RoleMenu>>(){}.getType();
                arrayList = gson.fromJson(result,listType);
                StringBuilder builder = new StringBuilder();
                if(arrayList.size()>0){
                    for (int i = 0;i<arrayList.size();i++){
                        builder.append(arrayList.get(i).getMenuId());
                        builder.append(",");
                    }
                }
                if(builder.lastIndexOf(",")!=-1){
                    builder.deleteCharAt(builder.lastIndexOf(","));
                }
                permission = builder.toString();
//                SharedPreferences.Editor editor = permissionPreferences.edit();
//                editor.putString("permission", permission);
//                editor.commit();
                AppPreferences appPreferences = new AppPreferences(MainActivity.this);
                appPreferences.put("permission",permission);
                //showMenu();
                //ToastUtil.show(LoginActivity.this,"permissions--->"+permission);
                if(permission!=null&&!permission.equals("")){
                    arr = permission.split(",");
                    // HashMap<String,Integer> permissionmap = FuncUtil.permissionMap;
                    HashMap<String,Integer> permissionmap = new HashMap<String,Integer>(){
                        {
                            put("44902f477395472d9494ee1309bb762a",R.id.distaskLayout);//任务下发
                            put("1eacf7913b204ad089a02dce0885827f",R.id.waittaskLayout);//代办任务
                            put("9a46dc829acb4915895f1366e610ab4c",R.id.histaskLayout);//历史任务
                            put("a408b4147d5540afa5786ef68b038231",R.id.populationLayout);//人口管理
                            put("954cc599ab524022859e3f5bff1325ff",R.id.cluelistLayout);//线索搜集
                            put("b98d4c9ef5f84286afb515074428f28e",R.id.mapLayout);//人员位置
                            put("f566cb1e1ccc411d9cd8a5f3c35a10d0",R.id.kaoheLayout);//人员考核
                            put("52b141ebc5c84781b7a40dd6b018ea4d",R.id.mytaskLayout);//我的任务
                            put("73727620627f4578b5660ede85afeb39",R.id.addpopulationLayout);// 人口上报
                            put("3ca31dd936e44759b957e05fde01b892",R.id.addclueLayout);// 线索上报
                        }
                    };

                    if(arr.length>0){
                        for(int i=0;i<arrayList.size();i++){
                            if(permissionmap.containsKey(arrayList.get(i).getMenuId())){
                                //permissionmap.get(arr[i]);
                                permissionmap.remove(arrayList.get(i).getMenuId());
                            }
                        }
                        for(String key : permissionmap.keySet()){
                            if(permissionmap.get(key)!=null){
                                View view = findViewById(permissionmap.get(key));
                                if(view!=null){
                                    view.setVisibility(View.GONE);
                                    //View views = getActivity().findViewById(permissionmap.get(key));
                                    mygrids.removeView(view);
                                }

                            }
                        }
                    }
                }else{
                    ToastUtil.show(MainActivity.this,"未获取权限列表");
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }


    private void showMenu(){
        if(permission!=null&&!permission.equals("")){
            arr = permission.split(",");
            //HashMap<String,Integer> permissionmap = FuncUtil.permissionMap;
            HashMap<String,Integer> permissionmap = new HashMap<String,Integer>(){
                {
                    put("44902f477395472d9494ee1309bb762a",R.id.distaskLayout);//任务下发
                    put("1eacf7913b204ad089a02dce0885827f",R.id.waittaskLayout);//代办任务
                    put("9a46dc829acb4915895f1366e610ab4c",R.id.histaskLayout);//历史任务
                    put("a408b4147d5540afa5786ef68b038231",R.id.populationLayout);//人口管理
                    put("954cc599ab524022859e3f5bff1325ff",R.id.cluelistLayout);//线索搜集
                    put("b98d4c9ef5f84286afb515074428f28e",R.id.mapLayout);//人员位置
                    put("f566cb1e1ccc411d9cd8a5f3c35a10d0",R.id.kaoheLayout);//人员考核
                    put("52b141ebc5c84781b7a40dd6b018ea4d",R.id.mytaskLayout);//我的任务
                    put("73727620627f4578b5660ede85afeb39",R.id.addpopulationLayout);// 人口上报
                    put("3ca31dd936e44759b957e05fde01b892",R.id.addclueLayout);// 线索上报
                }
            };

            if(arr.length>0){
                for(int i=0;i<arr.length;i++){
                    if(permissionmap.containsKey(arr[i])){
                        //permissionmap.get(arr[i]);
                        permissionmap.remove(arr[i]);
                    }
                }
                for(String key : permissionmap.keySet()){
                    if(permissionmap.get(key)!=null){
                        View view = findViewById(permissionmap.get(key));
                        if(view!=null){
                            view.setVisibility(View.GONE);
                            View views = findViewById(permissionmap.get(key));
                            mygrids.removeView(views);
                        }

                    }
                }
            }
        }else{
            ToastUtil.show(MainActivity.this,"未获取权限列表11");
        }
    }



    @OnClick(R.id.downtaskBtn)
    public void onDowntaskBtnClicked() {
        Intent i = new Intent();
        if(role_id.equals("73006b03b7f447bf953d065462e5b1ac")){
            i.setClass(MainActivity.this, DistributeActivity.class);
        }else{
            i.setClass(MainActivity.this, DistributeGridActivity.class);
        }

        startActivity(i);
    }

    @OnClick(R.id.waittaskBtn)
    public void onWaittaskBtnClicked() {
        Intent i = new Intent();
        i.setClass(MainActivity.this, WaittaskActivity.class);
        startActivity(i);
    }



    @OnClick(R.id.histaskBtn)
    public void onHistaskBtnClicked() {
        Intent i = new Intent();
        if(role_id.equals("73006b03b7f447bf953d065462e5b1ac")){
            i.setClass(MainActivity.this, HistaskActivity.class);
        }else{
            i.setClass(MainActivity.this, HisgridtaskActivity.class);
        }

        startActivity(i);
    }

    @OnClick(R.id.populationBtn)
    public void onPopulationBtnClicked() {
        Intent i = new Intent();
        if(role_id.equals("73006b03b7f447bf953d065462e5b1ac")){
            i.setClass(MainActivity.this, PopulationacountActivity.class);
        }else{
            Reportpopulation reportpopulation = new Reportpopulation();
            reportpopulation.setUnitid(Integer.valueOf(ssgrid));
            i.putExtra("reportpopulation",reportpopulation);
            i.setClass(MainActivity.this, PopulationActivity.class);
        }

        startActivity(i);
    }

    @OnClick(R.id.clueBtn)
    public void onClueBtnClicked() {
        Intent i = new Intent();

        if(role_id.equals("73006b03b7f447bf953d065462e5b1ac")){
            i.setClass(MainActivity.this, ClueAcountActivity.class);
        }else{
            Clue clue = new Clue();
            clue.setUnitid(ssgrid);
            i.putExtra("clue",clue);
            i.setClass(MainActivity.this, ClueActivity.class);
        }
        startActivity(i);
    }
//人员位置
    @OnClick(R.id.mapBtn)
    public void onMapBtnClicked() {
        Intent i = new Intent();
        i.setClass(MainActivity.this, MapActivity.class);
        startActivity(i);
    }
//人员考核
    @OnClick(R.id.kaoheBtn)
    public void onKaoheBtnClicked() {
        Intent i = new Intent();
        i.setClass(MainActivity.this, KaoheActivity.class);
        startActivity(i);
    }

    @OnClick(R.id.mytaskBtn)
    public void onMytaskBtnClicked() {
        Intent i = new Intent();
        i.setClass(MainActivity.this, MytaskActivity.class);
        startActivity(i);
    }

    @OnClick(R.id.addpopulationBtn)
    public void onAddpopulationClicked() {
        Intent i = new Intent();
        i.setClass(MainActivity.this, AddPopulationActivity.class);
        startActivity(i);
    }

    @OnClick(R.id.addclueBtn)
    public void onAddClueClicked() {
        Intent i = new Intent();
        i.setClass(MainActivity.this, AddClueActivity.class);
        startActivity(i);
    }


    @Override
    public void onBackPressed() {
        if (mBackPressed + TIME_INTERVAL > System.currentTimeMillis()) {
            super.onBackPressed();
            return;
        } else {
            Toast.makeText(getBaseContext(), "再次点击退出键退出", Toast.LENGTH_SHORT).show();
        }
        mBackPressed = System.currentTimeMillis();

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == R.id.action_logout) {
            AppPreferences appPreferences = new AppPreferences(MainActivity.this);
            appPreferences.remove("permission");
            appPreferences.clear();
            Intent i = new Intent(this, LoginActivity.class);
            startActivity(i);

//            app.exit();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}
